package com.example.api.service.impl;

import com.example.api.exception.ParamException;
import com.example.api.pojo.Food;
import com.example.api.service.CartService;
import com.example.api.service.FoodService;
import com.example.api.utils.CartItem;
import com.example.api.utils.StringUtil;
import com.example.api.utils.ThreadContextUtil;
import com.example.api.vo.FoodVO;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {

    @Resource
    RedisTemplate redisTemplate;

    @Resource
    FoodService foodService;

    @Override
    public void plus(int foodId, String flavorItem, String flavorValue) {

        Long customerId = ThreadContextUtil.getData();


        String key = "customer:"+customerId+":cart";

        String cartKey = "food:"+foodId;

        if(!StringUtil.isEmpty(flavorItem)){
            cartKey+=":"+flavorItem+":"+flavorValue;
        }

        HashOperations hashOperations = redisTemplate.opsForHash();


        CartItem cartItem = (CartItem) hashOperations.get(key,cartKey);
        if(cartItem==null){
            //加入
            cartItem = new CartItem();
            cartItem.setFoodId(foodId);

            FoodVO foodVO = foodService.findById(foodId);
            if(foodVO==null){
                throw new ParamException("菜品参数异常");
            }
            //TODO
            cartItem.setName(foodVO.getName());
            cartItem.setPhoto(foodVO.getPhoto());
            cartItem.setDescription(foodVO.getDescription());

            cartItem.setCount(1);
            cartItem.setPrice(foodVO.getPrice());

            cartItem.setFlavorItem(flavorItem);
            cartItem.setFlavorValue(flavorValue);

            hashOperations.put(key,cartKey,cartItem);
        }else{
            //获取到当前的数量
            //+1
            //更新
            cartItem.setCount(cartItem.getCount()+1);
            hashOperations.put(key,cartKey,cartItem);
        }
    }

    @Override
    public void minus(int foodId, String flavorItem, String flavorValue) {
        Long customerId = ThreadContextUtil.getData();

        String key = "customer:"+customerId+":cart";

        String cartKey = "food:"+foodId;

        if(!StringUtil.isEmpty(flavorItem)){
            cartKey+=":"+flavorItem+":"+flavorValue;
        }

        HashOperations hashOperations = redisTemplate.opsForHash();
        CartItem cartItem = (CartItem) hashOperations.get(key,cartKey);

        if(cartItem!=null){
            if(cartItem.getCount()<=1){
                //删除掉
                hashOperations.delete(key,cartKey);
            }else{
                cartItem.setCount(cartItem.getCount()-1);
                hashOperations.put(key,cartKey,cartItem);
            }
        }
    }

    @Override
    public List<CartItem> find() {
        List<CartItem> cartItemList = new ArrayList<>();

        Long customerId = ThreadContextUtil.getData();

        String key = "customer:"+customerId+":cart";
        HashOperations hashOperations = redisTemplate.opsForHash();

        Map<String,CartItem> cartInfo = hashOperations.entries(key);

        Collection<CartItem> collection = cartInfo.values();
        for(CartItem cartItem : collection){
            cartItemList.add(cartItem);
        }
        return cartItemList;
    }
}
